package com.zzptc.zzsgz.bdsjws.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.activities.UpData_Version;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpData;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lc on 2017/5/22.
 */

public class CheckUpdataVersion {

    public static void getCheckUpdataResponse(final Activity activity){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://119.29.227.211/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IVersionUpData service = retrofit.create(IVersionUpData.class);
        Call<VersionInfo> call=service.getVersionInfo();
        call.enqueue(new Callback<VersionInfo>() {
            @Override
            public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                //网络正常   可以下载~~~已经是最新版本
                Toast.makeText(activity, "下载！！！", Toast.LENGTH_SHORT).show();
                VersionInfo versionInfo=response.body();
                int netcode=versionInfo.getVersionCode();
                int oldcode=getVersionCode(MyApplication.getContext());
                if(netcode>oldcode){
                    Intent intent=new Intent();
                    intent.putExtra("VersionInfo",versionInfo);
                    activity.setResult(3,intent);
                    activity.finish();
                }else{
                    activity.setResult(4);
                    activity.finish();
                }


            }

            @Override
            public void onFailure(Call<VersionInfo> call, Throwable t) {
                //网络比较慢
                activity.setResult(4);
                activity.finish();
            }
        });
    }
    public static int getVersionCode(Context context)//获取版本号(内部识别号)
    {
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    public static boolean checkNet(Activity activity){
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
//            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            activity.setResult(1);
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
//            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            activity.setResult(2);
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
//            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            activity.setResult(3);
            return true;
        }
        return false;
    }


    public static boolean getCheckDateUtil(){
        SharedPreferences sharedPreferences=MyApplication.getContext().getSharedPreferences("checkDate", Context.MODE_PRIVATE);
        long time=sharedPreferences.getLong("dateInfo",0);
        long newTime=System.currentTimeMillis();
        sharedPreferences.edit().putLong("dateInfo",newTime).commit();
        if(time==0){
            return true;
        }else {
            if(newTime-time>=3600*24*1000*7){
                return true;
            }
        }
        return false;
    }
}
